/**
 * 简单API测试
 * 
 * 用途：直接测试API连接和数据获取
 * 方法：使用基本的HTTP请求测试API可达性
 * 原理：验证网络连接、API响应和数据解析
 */
package com.example.brain_wave.test.store_room

import kotlinx.coroutines.runBlocking
import org.junit.Test
import java.net.HttpURLConnection
import java.net.URL

class SimpleApiTest {
    
    @Test
    fun testDirectApiCall() {
        runBlocking {
            println("=== 开始直接API测试 ===")
            
            try {
                val apiUrl = "http://192.168.2.3:8000/admin/store/apiStores?limit=100&page=1"
                println("测试URL: $apiUrl")
                
                val url = URL(apiUrl)
                val connection = url.openConnection() as HttpURLConnection
                
                // 设置请求属性
                connection.requestMethod = "GET"
                connection.setRequestProperty("token", "jsw2hSS8ypfheSX5")
                connection.setRequestProperty("server", "1")
                connection.connectTimeout = 10000
                connection.readTimeout = 10000
                
                println("请求头设置完成")
                println("token: jsw2hSS8ypfheSX5")
                println("server: 1")
                
                // 发送请求
                val responseCode = connection.responseCode
                println("响应码: $responseCode")
                
                if (responseCode == HttpURLConnection.HTTP_OK) {
                    val response = connection.inputStream.bufferedReader().use { it.readText() }
                    println("响应长度: ${response.length}")
                    println("响应内容前500字符: ${response.take(500)}")
                    
                    // 检查是否包含南京门店
                    if (response.contains("南京")) {
                        println("✅ 找到南京门店数据！")
                    } else {
                        println("⚠️ 未在响应中找到南京门店")
                    }
                    
                    // 检查JSON结构
                    if (response.contains("\"code\":1")) {
                        println("✅ API返回成功状态")
                    } else {
                        println("❌ API返回失败状态")
                    }
                    
                } else {
                    println("❌ HTTP请求失败: $responseCode")
                    val errorResponse = connection.errorStream?.bufferedReader()?.use { it.readText() }
                    println("错误响应: $errorResponse")
                }
                
                connection.disconnect()
                
            } catch (e: Exception) {
                println("❌ 测试异常: ${e.message}")
                e.printStackTrace()
            }
            
            println("=== API测试完成 ===")
        }
    }
    
    @Test
    fun testNetworkReachability() {
        println("=== 测试网络可达性 ===")
        
        try {
            val host = "192.168.2.3"
            val port = 8000
            
            println("测试主机: $host:$port")
            
            val socket = java.net.Socket()
            socket.connect(java.net.InetSocketAddress(host, port), 5000)
            
            if (socket.isConnected) {
                println("✅ 网络连接成功")
                socket.close()
            } else {
                println("❌ 网络连接失败")
            }
            
        } catch (e: Exception) {
            println("❌ 网络测试异常: ${e.message}")
            println("可能的原因:")
            println("1. 设备未连接到正确的网络")
            println("2. IP地址不可达")
            println("3. 端口被防火墙阻止")
            println("4. 服务器未运行")
        }
        
        println("=== 网络测试完成 ===")
    }
}